<template>
  <div class="content">
    <div class="login">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号：" prop="username" class="item">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password" class="item">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="bottom">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
      return {
        ruleForm: {
          username: 'admin',
          password: '123456',
        },
        rules: {
         username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
           password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async(valid) => {
          if (valid) {//基本的校验完成
            let {data:{fail,token}} = await this.$axios.post("/login",this.ruleForm)
            if(fail){//fail:1，登录失败
                this.$message({
                  message: '密码不正确,或是没注册',
                  type: 'warning'
                });
            }else{ //fail:0,登录成功
              this.$message({
                  message: '恭喜你，登录成功',
                  type: 'warning'
                });
            //保存token
            window.sessionStorage.setItem("usertoken",token)
            //请求他人接口的数据  
            let {data:res} = await this.$axios.post("http://timemeetyou.com:8889/api/private/v1/login",{username:"admin",password:"123456"})
            //保存他人的token
            window.sessionStorage.setItem("token",res.data.token)
            this.$router.push("/admin")
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      async register(){
        let {data:{registerStatus}} = await this.$axios.post("/register",this.ruleForm)
        console.log(registerStatus)
        if(registerStatus){//registerstatus为1,注册成功
          this.$message({
            message: '恭喜你注册成功',
            type: 'success'
          });
        }else{//registerstatus为0,表示不用注册
          this.$message({
            message: '已有此账户，无法重新注册',
            type: 'warning'
          });
        }

      },
      resetForm(formName) {
          console.log(formName)
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style lang="less" scoped>
.content{
  width:100%;
  height: 100%;
  background-color: #61534c;
  .login{
    position:relative;
    width: 400px;
    height: 300px;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    .demo-ruleForm{
        position: absolute;
        top:20%;
        .item{
            width: 340px;
            height: 100%;
        }
        .bottom{
            display: flex;
            justify-content: flex-end;
        }
    }
    } 
}

</style>